﻿//功能描述：客户管理列表数据访问层
using Hong.IRepository.IRepository_CustomerManagement;
using Hong.Model.Dto.Model_CustomerManagementDto;
using Hong.Repository.Data;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;
using Dapper;
using Hong.Model.SysDomain.Model_CustomerManagement;
using Hong.Common.Encryption;

namespace Hong.Repository.Repository.Repository_ContractManagement
{
    public class CustInfoListRepository : ICustInfoListRepository
    {
        #region 构造器注入
        //注入解密SQL连接字符串
        private readonly SecurityStatic _securityStatic;
        public CustInfoListRepository(SecurityStatic securityStatic)
        {
            _securityStatic = securityStatic;
        }

        #endregion

        #region 客户信息列表

        #region 客户信息列表显示处查询

        #region 客户信息列表显示查询
        /// <summary>
        /// 客户信息列表显示查询
        /// </summary>
        /// <param name="customer"></param>
        /// <returns></returns>
        public List<CustomerExtendedView> GetCustomerExtendedDto(string custId, string custName, string custPhones, string edTypeId, string edIndustryId, int pageIndex, int pageSize, out int totalCount)
        {
            using (IDbConnection conn = new SqlConnection(_securityStatic.Securityinfo()))
            {
                var sql = "select a.*,b.*,c.dicName,c.dicId,d.dicName as dicName1,d.dicId as dicId1 from Customer a join Extended b on a.custId=b.customerId join Dictionaries c on b.edTypeId =c.dicId  join Dictionaries d on b.edIndustryId=d.dicId where 1=1 ";
                //客户编号
                if (custId != null)
                {
                    sql += $" and a.custId='{custId}'";
                }
                //客户名称
                if (custName != null)
                {
                    sql += $" and a.custName like '%{custName}%'";

                }
                //联系人
                if (custPhones != null)
                {
                    sql += $" and a.custPhones='{custPhones}'";

                }
                //客户类型
                if (edTypeId != null)
                {
                    sql += $" and b.edTypeId='{edTypeId}'";

                }
                //所属行业
                if (edIndustryId != null)
                {
                    sql += $" and b.edIndustryId='{edIndustryId}'";
                }
                totalCount = conn.Query<CustomerExtendedView>(sql).Count();
                sql += $" order by a.custId offset {pageIndex - 1}*{pageSize} rows fetch next {pageSize} row only";
                var i = conn.Query<CustomerExtendedView>(sql).ToList();
                return i;
            }

        }

        #endregion

        #region 批量删除客户信息
        /// <summary>
        /// 批量删除客户信息
        /// </summary>
        /// <returns></returns>
        public int PDeleteCustomer(string ids)
        {
            using (IDbConnection conn = new SqlConnection(_securityStatic.Securityinfo()))
            {
                string[] str = ids.Split(',');
                foreach (var item in str)
                {
                    var sql = $"delete from Customer where custId='{item}'";
                    conn.Execute(sql);

                }
                return 1;
            }
        }
        #endregion

        #region 修改客户列表信息

        /// <summary>
        /// 修改客户列表信息
        /// </summary>
        /// <param name="customer"></param>
        /// <returns></returns>
        public int PutCustomer(Customer customer)
        {
            using (IDbConnection conn = new SqlConnection(_securityStatic.Securityinfo()))
            {
                var sql = $"update Customer set custName='{customer.CustName}',custEmail='{customer.CustEmail}',custPhone='{customer.CustPhone}',custAbbreviation='{customer.CustAbbreviation}',custAddress='{customer.CustAddress}',custPhones='{customer.CustPhones}',custFaxNumber='{customer.CustFaxNumber}' where custId='{customer.CustId}'";
                conn.Execute(sql);
                return 1;
            }
        }
        #endregion

        #endregion

        #region 客户信息列表反填
        #region 反填客户列表信息
        /// <summary>
        /// 反填客户列表信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Customer GetCustomerFT(string id)
        {
            using (IDbConnection conn = new SqlConnection(_securityStatic.Securityinfo()))
            {
                var sql = $"select * from Customer where custId='{id}' ";
                var list = conn.Query<Customer>(sql).FirstOrDefault();
                return list;
            }
        }
        #endregion

        #region 扩展信息人反填
        /// <summary>
        /// 扩展信息人反填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Extended GetSatisfactionFT(string id)
        {
            using (IDbConnection conn = new SqlConnection(_securityStatic.Securityinfo()))
            {
                var sql = $"select * from Extended where customerId='{id}' ";
                var list = conn.Query<Extended>(sql).FirstOrDefault();
                return list;
            }
        }
        #endregion

        #endregion

        #endregion

    }
}
